import type { IMenu } from '../../store/menu'
import { Tree } from '@pkgs/utils'
import useMenuStore from '../../store/menu'
import useMicroStore from '../../store/micro'

export const vPermission = {
  name: 'permission',
  created: (el: HTMLElement, binding: any) => {
    const microStore = useMicroStore()
    const menuStore = useMenuStore()

    const menus = microStore.getIsMicro
      ? microStore.getProps.menus
      : menuStore.getMenus

    const menu = new Tree<IMenu>().findNode(menus, node => node?.code?.includes(binding?.value))

    if (!menu) {
      el.style.display = 'none'
    }
    else {
      el.style.display = ''
    }
  },
}
